package com.td.module.produce.controller.app.husbandry;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.produce.controller.admin.husbandry.vo.*;
import com.td.module.produce.convert.husbandry.HusbandryConvert;
import com.td.module.produce.dal.dataobject.husbandry.HusbandryDO;
import com.td.module.produce.service.husbandry.HusbandryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "用户APP - 生产管理农事")
@RestController
@RequestMapping("/produce/husbandry")
@Validated
public class AppHusbandryController {

    @Resource
    private HusbandryService husbandryService;

    @PostMapping("/create")
    @ApiOperation("创建生产管理农事")
    public CommonResult<Long> createHusbandry(@Valid @RequestBody HusbandryCreateReqVO createReqVO) {
        return success(husbandryService.createHusbandry(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新生产管理农事")
    public CommonResult<Boolean> updateHusbandry(@Valid @RequestBody HusbandryUpdateReqVO updateReqVO) {
        husbandryService.updateHusbandry(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除生产管理农事")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteHusbandry(@RequestParam("id") Long id) {
        husbandryService.deleteHusbandry(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得生产管理农事")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<HusbandryUpdateReqVO> getHusbandry(@RequestParam("id") Long id) {
        HusbandryUpdateReqVO husbandry = husbandryService.getHusbandry(id);
        return success(husbandry);
    }

    @GetMapping("/list-infoId")
    @ApiOperation("根据生产id获得生产管理农事列表")
    @ApiImplicitParam(name = "infoId", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<List<HusbandryRespVO>> getHusbandryListByInfoId(@RequestParam("infoId") Long infoId) {
        List<HusbandryDO> husbandry = husbandryService.getHusbandryListByInfoId(infoId);
        return success(HusbandryConvert.INSTANCE.convertList(husbandry));
    }

    @GetMapping("/list")
    @ApiOperation("获得生产管理农事列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<HusbandryRespVO>> getHusbandryList(@RequestParam("ids") Collection<Long> ids) {
        List<HusbandryDO> list = husbandryService.getHusbandryList(ids);
        return success(HusbandryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得生产管理农事分页")
    public CommonResult<PageResult<HusbandryRespVO>> getHusbandryPage(@Valid HusbandryPageReqVO pageVO) {
        PageResult<HusbandryDO> pageResult = husbandryService.getHusbandryPage(pageVO);
        return success(HusbandryConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出生产管理农事 Excel")
    @OperateLog(type = EXPORT)
    public void exportHusbandryExcel(@Valid HusbandryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HusbandryDO> list = husbandryService.getHusbandryList(exportReqVO);
        // 导出 Excel
        List<HusbandryExcelVO> datas = HusbandryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "生产管理农事.xls", "数据", HusbandryExcelVO.class, datas);
    }

}
